<!doctype html>
<html>
<head>
    <meta charset="UTF-8">
    <title></title>
    <meta name="viewport"
          content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no"/>
    <link href="../../libs/mui.min.css" rel="stylesheet"/>
    <link href="../../libs/app.css" rel="stylesheet"/>
    <link href="../../css/main.css" rel="stylesheet"/>
    <link href="../../css/evaluate.css" rel="stylesheet"/>
    <link href="../../css/perfect.css" rel="stylesheet"/>
</head>
<body>
<div class="head">
    <img id="back" class="mui-action-back" src="../../images/all-left.png" alt="">
    <p>评估结果</p>
</div>
<div class="context">
    <div class="wish">
        <p class="wish-1"></p>
        <p class="wish-2"></p>
    </div>
    <div class="head-c">
        <img src="../../images/chi-up.png" alt="">
        <div class="mine">
            <div class="mine-photo"></div>
            <p id="name"></p>
            <img src="" id="vip" alt="">
            <p>根据您的申请信息，可申请的金额：</p>
            <p>
                <spam class="rs"></spam>
                万元
            </p>
        </div>
        <img src="../../images/chi-down.png" alt="">
    </div>
    <!--<p class="suss-save">请您先选择保存评估，再进行该项贷款申请！</p>-->
    <div class="choose-assess">
        <p class="recommend-der">根据您填写的信息，我们为你推荐以下评估：</p>
        <!--<div class="success-if">
            <div class="mui-card">
                <div class="mui-card-content">
                    <div class="policy">人寿保单贷</div>
                    <div class="time">贷款期限1~3年</div>
                    <div class="accrual">月利率：0.86%</div>
                    <div class="apply">
                        <span>马上申请</span>
                        <img src="../../images/jian1.png" />
                    </div>
                </div>
            </div>
        </div>-->
    </div>

    <div class="button">
        <!--<button id="go-home">返回主页</button>-->
        <button id="save">直接保存</button>
        <button id="informa">立即申请</button>
        <!--<button id="informa">去完善资料</button>-->
    </div>
</div>
<script src="../../libs/mui.min.js"></script>
<script src="../../js/jquery-3.1.1.min.js"></script>
<script src="../../js/url.js"></script>
<script src="../../js/userInfo.js"></script>
<script>

    if (localStorage.getItem('assess-evaluate') == 'assess-evaluate') $('#informa').remove();
    localStorage.removeItem('assess-evaluate');

    //判断上一级页面是谁决定保存按钮的去留
    if (localStorage.thisWebId === 'mine-assess') {
        $('#save').remove();
        localStorage.removeItem('thisWebId');
    } else {
        //每次到评估结果页都关闭前面打开的页面
//        mui.plusReady(function () {
//            $.closePages(['evaluate']);
//        });
    }
    var Fail = {};
    //快速贷推荐该银行的其他未评估过的评估
    Fail.fastRecommend = function () {
        $.ajax({
            type: "get",
            url: ur + "rest/quickLoanController/quick-evaluate",
            async: true,
            headers: {
                'X-Token': localStorage.token
            },
            data: {
                id: localStorage.fast_id
            },
            success: function (data) {
                console.log('快速贷推荐该银行的其他未评估过的评估', data);
                var data = JSON.parse(data);
                if (data.meta.code == 'S002') {
                    var list = data.data;
                    var len;
                    for (var j = 0; j < list.length; j++) {
                        // 去除现在的类型，推荐其他的业务类型
                        if (localStorage.loanCode == list[j].code) {
                            list.splice(j, 1);
                        }
                    }
                    list.length <= 2 ? len = list.length : len = 2; //最多推荐2条
                    for (var i = 0; i < len; i++) {
                        var otherAssess = '<div class="success-if"><div class="mui-card"><div class="mui-card-content"><div class="policy">' +
                            list[i].name + '</div><div class="time">贷款期限1~3年</div><div class="accrual">月利率：0.86%</div><div class="apply" key="' +
                            list[i].code + '"><span>马上申请</span><img src="../../images/jian1.png" /></div></div></div></div>';
                        $('.choose-assess').append(otherAssess);
                    }
                } else {
                    mui.toast(data.meta.message);
                }
            }
        });
    }
    // 普通贷推荐未评估过的评估
    Fail.failRec = function () {
        $.ajax({
            url: ur + 'rest/loanController/not-evaluate',
            type: 'get',
            dataType: 'json',
            headers: {
                "X-Token": localStorage.token
            },
            success: function (data) {
                console.log('普通贷推荐未评估过的评估', data);
                if (data.meta.code === 'S002') {
                    var list = data.data;
                    var maxLen; // 推荐条数
                    list.length <= 2 ? maxLen = list.length : maxLen = 2; //最多推荐2条

                    $('.recommend-der').show(0);

                    //去除当前已评估失败的未评估业务类型
                    for (var j = 0; j < list.length; j++) {
                        if (list[j].code == localStorage.loanCode) {
                            list.splice(j, 1);
                        }
                    }

                    var showList = Fail.getArrayItems(list, maxLen);

                    for (var i in showList) {
                        var otherAssess = '<div class="success-if"><div class="mui-card"><div class="mui-card-content"><div class="policy">' +
                            showList[i].name + '</div><div class="time">贷款期限1~3年</div><div class="accrual">月利率：0.86%</div><div class="apply" key="' +
                            showList[i].code + '"><span>马上申请</span><img src="../../images/jian1.png"/></div></div></div></div>';
                        $('.choose-assess').append(otherAssess);
                    }
                } else {
                    mui.toast(data.meta.message);
                }
            }
        })
        localStorage.removeItem('fast_id')
    }
    //    Fail.noR = function (a, b, n) { //去除重复的推荐随机数
    //        if (a) {
    //            if (a != b) {
    //                b = b;
    //            } else {
    //                b = Math.floor(Math.random() * n)
    //                Fail.noR(a, b, n)
    //            }
    //        }
    //    }
    Fail.getArrayItems = function (arr, num) { // 随机取出特定数量的不重复数组项
        var copy_arr = new Array();
        for (var index in arr) {
            copy_arr.push(arr[index]);
        }
        var return_arr = new Array();
        for (var i = 0; i < num; i++) {
            if (copy_arr.length > 0) {
                var arrIndex = Math.floor(Math.random() * copy_arr.length);
                return_arr[i] = copy_arr[arrIndex];
                copy_arr.splice(arrIndex, 1);
            } else {
                break;
            }
        }
        return return_arr;
    }

    //推荐评估点击跳转相应评估信息页
    $(function () {
        //循环关闭打开窗口  返回直接首页
        $('#back').on('tap', function () {
//            $.closePages();
            href('../head-foot-demo.html#/home');
        });
        mui.init({
            beforeback: function () {
//                $.closePages();
                href('../head-foot-demo.html#/home');
                return true;
            }
        });
        // 未通过 马上申请btn
        $('.choose-assess').on('tap', '.apply', function () {
            // $.closePages(['evaluate']);
            switch ($(this).attr('key')) {
                case 'rsbd':
//                    href('one-life.html', 'one-life', {hold: 'one-life'});
                    href('one-life.html', 'one-life');
                    localStorage.setItem('evaluate-oneLife', JSON.stringify({hold: 'one-life'}));
                    break;
                case 'fdyg':
                    href('one-housing.html', 'one-housing', {
                        hold: 'one-housing'
                    });
                    break;
                case 'ylzy':
                    href('one-professional.html', 'one-professional', {
                        hold: 'one-professional'
                    });
                    break;
                case 'gsjx':
                    href('one-corporate.html', 'one-corporate', {
                        hold: 'one-corporate'
                    });
                    break;
                case 'fcdy':
                    href('two-house.html', 'two-house', {
                        hold: 'two-house'
                    });
                    break;
                case 'qcdy':
                    href('two-car.html', 'two-car', {
                        hold: 'two-car'
                    });
                    break;
                case 'aj':
                    href('four-aj.html', 'four-aj', {
                        hold: 'four-aj'
                    });
                    break;
                case '2y':
                    href('three-2y.html', 'three-2y', {
                        hold: 'three-2y'
                    });
                    break;
            }
        });
    });

    //判断评估结果显示内容
    if (localStorage.assess === 'fail') {
        $('.wish-1').text('很遗憾！');
        $('.wish-2').text('未通过评估');
        $('#informa').remove();

        if (localStorage.fast_id) {
            /*window.addEventListener('fastRecommend', function(event) {
             Fail.fastRecommend(event.detail.id);
             })*/
            Fail.fastRecommend() // 评估失败 快速贷推荐
        } else {
            Fail.failRec() // 普通贷推荐
        }

        /*//缓存评估失败的业务类型
         sessionStorage.failObj+=','+localStorage.rsbd;
         Fail.Obj=sessionStorage.failObj.split(',');*/

    } else {
        $('.wish-1').text('恭喜您!');
        $('.wish-2').text('通过评估');
    }

    // 本地获取
    var token = localStorage.token;
    var rsbd = localStorage.rsbd;
    var param = localStorage.params;
    var rs = localStorage.rs;
    $('.rs').text(rs);

    // 通过评估，点击立即申请： 自动保存
    var tapSave = 1;
    $('#informa').on('tap', function () {
        if (tapSave == 1) {
            $.ajax({
                type: "post",
                url: ur + "rest/loanController/saveEvaluate",
                headers: {
                    'X-Token': token
                },
                dataType: "json",
                data: {
                    loanCode: rsbd,
                    param: param,
                    rs: rs
                },
                success: function (data) {
                    // mui.toast('保存成功');
                }
            });
        }
        switch (localStorage.rsbd) {
            case 'fcdy':
                href('pledge.html', 'pledeg');
                break;
            case 'qcdy':
                href('car.html', 'car');
                break;
            case 'aj':
                href('pay-in.html', 'pay-in');
                break;
            case '2y':
                href('erya.html', 'erya');
                break;
            case 'ylzy': // 优良职业贷
                href('credit.html', 'credit');
                localStorage.setItem('ylzy_tab', 2);
                break;
            default:
                href('personal.html', 'personal.html');
        }
    });

    // 保存：返回首页
    $('#save').on('tap', function () {
        if (token == null) {
            mui.toast('请先登录...')
        } else {
            if (tapSave === 1) {
                $.ajax({
                    type: "post",
                    url: ur + "rest/loanController/saveEvaluate",
                    headers: {
                        'X-Token': token
                    },
                    dataType: "json",
                    data: {
                        loanCode: rsbd,
                        param: param,
                        rs: rs
                    },
                    success: function (data) {
                        mui.toast('保存成功');
                    }
                });
            } else {
                mui.toast('您已保存！')
            }
            tapSave += 1;
        }
    })

    //个人消息
    localStorage.thisUser = 'save'; //时候保留保存按钮
    var saveUser = new User();
    saveUser.getMessage();
</script>
</body>

</html>